unit clsCausas_Parada;

interface

uses Dm,dbtables,biblio;

type TCausas_Parada =Class(TObject)
    public
      Descricao_causa : String;
      Status          : string; // P-Parada
                                // A-Atividade
                                // R-Retrabalho
                                // L-Perdido
                                // D-Retrabalho
      function  Gravar(P_cod_causa:String)   : Byte;
      function  Recuperar(P_cod_causa:String): Byte;
      function  Excluir(P_cod_causa:String)  : Byte;
      procedure Recuperar_Todas(var tq: TQuery);
      procedure Consultar(var p_q:TQuery; P_descricao_causa:String);
  end;

implementation

{ TCausas_Parada }

procedure TCausas_Parada.Consultar(var p_q: TQuery;
  P_descricao_causa: String);
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from causas_parada where descricao_causa>=:k_descricao_causa order by descricao_causa ');
  p_q.parambyname('k_descricao_causa').asstring := P_descricao_causa;
end;

function TCausas_Parada.Excluir(P_cod_causa: String): Byte;
var
  tq :TQuery;
begin
  tq := TQuery.create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tq.sql.clear;
  tq.Params.clear;
  tq.sql.add('Delete from causas_parada where cod_causa=:K_cod_causa');
  tq.ParamByName('K_cod_causa').AsString := P_Cod_Causa;
  tq.ExecSQL;
  tq.Close;
  tq.free;
  result:= 1;
end;

function TCausas_Parada.Gravar(P_cod_causa: String): Byte;
var tq:TQuery;
begin
  tq             := Tquery.Create(DmInt);
  tq.databasename:= dmint.Db.DatabaseName;
  tq.active      := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM causas_parada where cod_causa=:k_cod_causa');
  tq.parambyname('k_cod_causa').asstring:= StrZero(P_cod_causa,2);
  tq.open;
  if tq.eof then begin
   tq.sql.clear;
   tq.params.clear;
   tq.sql.add('INSERT INTO causas_parada VALUES(:k_cod_causa ,');
              if Descricao_causa <>'' then
                tq.sql.add(':k_descricao_causa,')
              else
                tq.sql.add('null,');
              if Status <> '' then
                tq.sql.add(':K_status)')
              else
                tq.sql.add('null)');
  end else begin
   tq.sql.clear;
   tq.params.clear;
   tq.sql.add('UPDATE causas_parada SET ');
              if Descricao_causa <> '' then
               tq.sql.add('descricao_causa=:k_descricao_causa,')
              else
               tq.sql.add('descricao_causa=null ');
              if Status <> '' then
               tq.sql.add('status=:k_status ')
              else
               tq.sql.add('status=null ');
              tq.sql.add(' where cod_causa=:K_cod_causa');
  end;
  tq.parambyname('k_cod_causa').asstring := P_cod_causa;
  if Descricao_causa <>'' then
    tq.parambyname('k_descricao_causa').asstring:= Descricao_causa;
  if Status <>'' then
    tq.parambyname('k_status').asstring:= Status;
  tq.close;
  tq.execsql;
  result:= 1;
  tq.close;
  tq.Free;
end;

function TCausas_Parada.Recuperar(P_cod_causa: String): Byte;
var
  tq: tquery;
begin
  tq             := Tquery.Create(DmInt);
  tq.databasename:= dmint.Db.DatabaseName;
  tq.active      := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM causas_parada where cod_causa=:k_cod_causa');
  tq.parambyname('k_cod_causa').asstring:= P_cod_causa;
  tq.open;
  if tq.eof then
  begin
    Descricao_causa := '';
    Status          := '';
    Result := 0;
  end
  else
  begin
    Descricao_causa := tq.fieldbyname('descricao_causa').AsString;
    Status          := tq.fieldbyname('status').AsString;
    Result := 1;
  end;
  tq.close;
  tq.free;
end;

procedure TCausas_Parada.Recuperar_Todas;
begin
 tq.databasename:= dmint.db.databasename;
 tq.close;
 tq.sql.clear;
 tq.sql.add('Select * from Causas_Parada order by Cod_Causa');
 tq.open;
end;

end.
